package com.gang.ssm.bos.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gang.ssm.bos.common.Page;
import com.gang.ssm.bos.system.bean.Decidedzone;
import com.gang.ssm.bos.system.mapper.DecidedzoneMapper;
import com.gang.ssm.bos.system.mapper.SubareaMapper;
import com.gang.ssm.bos.system.service.DecidedzoneService;


@Service
public class DecidedzoneServiceImpl implements DecidedzoneService{

	@Autowired
	private DecidedzoneMapper decidedzoneMapper;
	
	@Autowired
	private SubareaMapper subareaMapper;
	
	@Override
	public void save(Decidedzone decidedzone,String[] subareaId) {
		if(decidedzone.getId() != null) {
			decidedzoneMapper.updateByPrimaryKeySelective(decidedzone);
			//先把原来已关联的定区记录删除
			subareaMapper.updateDecidedzoneId(decidedzone.getId());
		}else{
			decidedzoneMapper.insertSelective(decidedzone);
		}
		
		if(subareaId != null && subareaId.length > 0) {
			List<Integer> idList = new ArrayList<Integer>();
			for(String sId : subareaId) {
				idList.add(Integer.valueOf(sId));
			}
			subareaMapper.saveDecidedzoneId(decidedzone.getId(),idList);
		}
	}

	@Override
	public void pageQuery(Page<Decidedzone> pageBean) {
		int total = (int) decidedzoneMapper.countByExample(null);
		List<Decidedzone> decidedzones = decidedzoneMapper.findByLimit((pageBean.getPageNo() - 1) * 
				pageBean.getPageSize(),pageBean.getPageSize());
		
		pageBean.setTotalCount(total);
		pageBean.setResult(decidedzones);
	}

	@Override
	public List<Decidedzone> findRelationSubarea() {
		return decidedzoneMapper.selectRelationSubarea();
	}

	@Override
	public Decidedzone findWithSubareasById(Integer id) {
		return decidedzoneMapper.findWithSubareasById(id);
	}

	@Override
	public void deleteById(Integer id) {
		decidedzoneMapper.deleteByPrimaryKey(id);
	}
	
	

}
